<template>
  <div>
    <el-container>
      <el-aside width="220px">
        <div style="padding-top: 22px;padding-left: 8px;">
          <a @click="handleNodeClick('root')" class="ljm-org">组织机构</a>
          <el-tree
            style="margin-top: 6px;"
            :data="data"
            :props="defaultProps"
            :load="loadNode"
            lazy
            @node-click="handleNodeClick"
          />
        </div>
      </el-aside>
      <el-container>
        <el-main>
          <div>
            <div class="filter-container">
              <el-input v-model="listQuery.loginName" placeholder="用户账号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
              <el-input v-model="listQuery.realName" placeholder="用户名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
              <el-input v-model="listQuery.identityNo" placeholder="身份证号码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
              <el-input v-model="listQuery.email" placeholder="邮箱" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
              <div style="float:right;">
                <el-button v-if="checkPermission(['org:user:add'])" class="filter-item" style="margin-left: 10px;" icon="el-icon-edit" @click="handleCreate">新增</el-button>
                <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
              </div>
            </div>

            <el-table
              :key="tableKey"
              v-loading="listLoading"
              :data="list"
              stripe
              fit
              highlight-current-row
              style="margin-left: 1px;width: 100%;border-top: 1px solid #dfe6ec;border-left: 1px solid #dfe6ec;border-right: 1px solid #dfe6ec;"
            >
              <el-table-column label="名称" align="left">
                <template slot-scope="scope">
                  <span>{{ scope.row.realName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="职位" align="left" show-overflow-tooltip>
                <template slot-scope="scope">
                  <span v-for="item in positions">
                    <span v-if="item.itemKey == scope.row.position">{{ item.itemValue }}</span>
                  </span>
                </template>
              </el-table-column>
              <el-table-column label="账号" align="left" show-overflow-tooltip>
                <template slot-scope="scope">
                  <span>{{ scope.row.loginName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="身份证" align="left" show-overflow-tooltip>
                <template slot-scope="scope">
                  <span>{{ scope.row.identityNo }}</span>
                </template>
              </el-table-column>
              <el-table-column label="联系方式" align="left" show-overflow-tooltip>
                <template slot-scope="scope">
                  <span>{{ scope.row.mobile }}</span>
                </template>
              </el-table-column>
              <el-table-column label="邮箱" align="left" show-overflow-tooltip>
                <template slot-scope="scope">
                  <span>{{ scope.row.email }}</span>
                </template>
              </el-table-column>
              <el-table-column label="状态" align="center">
                <template slot-scope="scope">
                  <el-tag :type="scope.row.status | statusTypeFilter">
                    {{ scope.row.status | statusFilter }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column v-if="checkPermission(['org:user:view','org:user:edit','org:user:del'])" label="操作" align="center" width="160" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-tooltip v-if="checkPermission(['org:user:view'])" class="item action-icon" effect="dark" content="查看详情" placement="top">
                    <a><svg-icon icon-class="ljm-view" class="action_icon icon-size" @click="handleView(scope.row)" /></a>
                  </el-tooltip>
                  <el-tooltip v-if="checkPermission(['org:user:edit'])" class="item action-icon" effect="dark" content="编辑" placement="top">
                    <a><svg-icon icon-class="ljm-edit" class="action_icon icon-size" @click="handleUpdate(scope.row)" /></a>
                  </el-tooltip>
                  <el-tooltip v-if="checkPermission(['org:user:del'])" class="item action-icon" effect="dark" content="删除" placement="top">
                    <a><svg-icon icon-class="ljm-del" class="action_icon icon-size" @click="handleDelete(scope.row)" /></a>
                  </el-tooltip>
                </template>
              </el-table-column>
            </el-table>
            <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import {getChildDept, getRootDept} from '@/api/org/dept/index'
  import {delData, getList} from '@/api/org/user/index'
  import {getItemByDictKey} from '@/api/sys/dict/item/index'

  export default {
  name: 'user',
  components: { Pagination },
  filters: {
    statusTypeFilter(status) {
      const statusTypeMap = {
        1: 'primary',
        0: 'danger'
      }
      return statusTypeMap[status]
    },
    statusFilter(status) {
      const statusMap = {
        1: '启用',
        0: '禁用'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        fullName: '',
        loginName: '',
        realName: '',
        identityNo: '',
        email: ''
      },
      data: [],
      defaultProps: {
        children: 'children',
        label: 'deptName',
        isLeaf: 'leaf'
      },
      positions: []
    }
  },
  created() {
    this.getRootDept()
    this.getPosition('position')
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
      this.listLoading = false
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ name: 'user_mgr_add' })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        delData([row.id]).then(() => {
          this.getList()
          this.listLoading = false
        }).catch(
          this.listLoading = false
        )
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleUpdate(row) {
      this.$router.push({ name: 'user_mgr_edit', query: { id: row.id }})
    },
    handleView(row) {
      this.$router.push({ name: 'user_mgr_view', query: { id: row.id }})
    },
    handleNodeClick(data) {
      if (data === 'root') {
        this.listQuery.fullName = ''
      } else {
        this.listQuery.fullName = data.fullName
      }
      this.handleFilter()
    },
    getRootDept() {
      getRootDept().then(res => {
        this.data = res.data
      })
    },
    loadNode(node, resolve) {
      if (node.level === 0) {
        return resolve([{ name: 'ROOT' }])
      }
      if (node.level > 1) return resolve([])
      getChildDept(node.data.id).then(res => {
        resolve(res.data)
      })
    },
    getPosition(dictKey) {
      getItemByDictKey(dictKey).then(res => {
        this.positions = res.data
      })
    }
  }
}
</script>

<style>
body > .el-container {
  margin-bottom: 40px;
}
.action-icon{
  padding: 0 5px;
}
.icon-size{
  width: 17px!important;
  height: 17px!important;
}
.el-aside {
  overflow: auto;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -ms-flex-negative: 0;
  flex-shrink: 0;
  border-right: 1px solid #dfe6ec;
  min-height: calc(100vh - 50px);
  width: 100%;
  position: relative;
  overflow: hidden;
}

.el-tree-node__content {
  line-height: 36px!important;
  height: 36px!important;
  cursor: pointer!important;
  font-size: 14px!important;
}

.el-tree-node__label:before {
  content: "";
  display: inline-block;
  width: 16px;
  height: 14px;
  background: url(img/dept.png) no-repeat 50%;
  background-size: 16px 14px;
  margin-right: 10px;
  vertical-align: middle;
  margin-top: -3px;
}

.ljm-org{
  color:#757373;
  padding-left: 10px;
  font-size: 16px;
}

.ljm-org:before {
  content: "";
  display: inline-block;
  width: 16px;
  height: 14px;
  background: url(img/org.png) no-repeat 50%;
  background-size: 16px 14px;
  margin-right: 10px;
  vertical-align: middle;
  margin-top: -3px;
}
</style>
